import os.path
from bs4 import BeautifulSoup
import requests

origin_url = "https://pic.netbian.com/"
url = "https://pic.netbian.com/4kmeinv/index_3.html"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}


def spider_img(origin_url, url):
    res = requests.get(url, headers=headers)
    res.encoding = 'gbk'
    print(res.status_code)

    img_list = BeautifulSoup(res.text, 'html.parser')

    for img in img_list.find_all('img'):
        if '/uploads/' not in img['src']:
            continue
        print(img['src'])
        src = origin_url + img['src']
        filename = os.path.basename(src)
        if not os.path.exists('img'):
            os.mkdir('img')
        # wb 表示,w表示写入,b=bit 也就是二进制,意思就是以二进制写入
        with open(f"img/{filename}", 'wb') as f:
            res_img = requests.get(src, headers=headers)
            # content 代表将文件二进制流写入
            print(src)
            f.write(res_img.content)


for i in range(3, 300):
    url = f"https://pic.netbian.com/4kmeinv/index_{i}.html"
    spider_img(origin_url, url)
